<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}



h2 {
    font-size: 16px;
    margin-bottom: 20px;
    display: inline-block;
}

.wrap {
    padding: 30px;
    clear: both;
    border-bottom: 1px solid #dddee1;
}

.formdiv {
    clear: both;
    margin-bottom: 20px;
}

.mt20 {
    margin-top: 20px;
}

.mb20 {
    margin-bottom: 20px;
}
.line{
    margin-top: 20px;
    margin-bottom: 20px;
    width: 100%;
    height: 1px;
    border-bottom: 1px dotted #dddee1;
}
.upload-policy div{
    display: inline-block;
}
</style>
<style>
.clearfix:after {
    visibility: hidden;
    display: block;
    content: " ";
    clear: both;
    height: 0;
    line-height: 0;
}

.clearfix {
    zoom: 1;
}
</style>

<template>
    <div class="policy-edit">
        <product-title></product-title>
        <div class="wrap">
            <Row>
                <Col span="3">
                    <h2>
                        保单信息     
                    </h2>
                </Col>
                <Col span="20" offset="1" class="upload-policy">
                    <!-- <Button type="primary" @click="handleSubmit()">提交</Button> -->
                    <Upload
                        :before-upload="handleUpload"
                        accept="image/*"
                        action="">
                        <Button type="primary" icon="ios-cloud-upload-outline">保单识别(仅泰康保单可用)</Button>
                    </Upload>
                    <div v-if="file !== null">文件名: {{ file.name }} <Button type="success" @click="upload" :loading="loadingStatus">{{ loadingStatus ? '上传中...' : '点击上传' }}</Button></div>
                </Col>
            </Row>
            <Form class="clearfix" :model="policyDetail" :ref="policyDetail" :rules="ruleValidate" :label-width="120">
                <Row>
                    <Col span="8" offset="1">
                    <Form-item label="保单类型：" >
                        <Select v-model="policyDetail.type" placeholder="请选择类型"  style="width:140px">
                            <Option  value="人寿险">人寿险</Option>
                            <Option  value="意外险">意外险</Option>
                            <Option  value="重疾险">重疾险</Option>
                            <Option  value="医疗险">医疗险</Option>
                            <Option  value="年金险">年金险</Option>
                            <Option  value="综合型">综合型</Option>
                        </Select>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="保险公司：" required>
                        <Select v-model="policyDetail.cname" placeholder="请选择保险公司" >
                            <Option  :value="item.name" v-for="item in comlist" :key="item.name">{{item.name}}</Option>
                        </Select>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="保单编号：" required>
                        <Input v-model="policyDetail.num" placeholder="请输入保单编号"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="保障计划：" required>
                        <Input v-model="policyDetail.name" placeholder="请输入保障计划"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="保单生效日：" required>
                        <DatePicker type="datetime" v-model="policyDetail.inception_date_format"  @on-change="inceptionDateChange" placeholder="选择日期"></DatePicker>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="状态：">
                        <!-- <Input v-model="policyDetail.status" placeholder="请输入状态"></Input> -->
                        <Select v-model="policyDetail.status" placeholder="请选择保单状态" >
                            <Option  value="正常">正常</Option>
                            <Option  value="终止">终止</Option>
                        </Select>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="年化保费">
                        <Input v-model="policyDetail.totalRate" placeholder="请输入年化保费"></Input>
                    </Form-item>
                    </Col>
                </Row>
            </Form>
        </div>
        <!-- 投保人 -->
        <div class="wrap">
            <h2>
                投保人信息
            </h2>
            <Form class="clearfix" :model="policyDetail.applicant" :ref="policyDetail.applicant" :rules="ruleValidate" :label-width="140">
                <Row>
                    <Col span="4" offset="4">
                        <Button type="text" style='color:blue;padding-left:0' @click='insertApp'>导入投保人</Button>
                    </Col>
                </Row>
                <Row>
                    <Col span="8" offset="1">
                    <Form-item label="投保人姓名：" required>
                        <Input v-model="policyDetail.applicant.appName" placeholder="请输入投保人姓名"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="手机号：" required>
                        <Input v-model="policyDetail.applicant.appMobile" placeholder="请输入手机号"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="身份证：" required>
                        <Input v-model="policyDetail.applicant.appNum" placeholder="请输入身份证号"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="电子邮箱：">
                        <Input v-model="policyDetail.applicant.appEmail" placeholder="请输入电子邮箱"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="联系地址：">
                        <Input v-model="policyDetail.applicant.addr" placeholder="请输入联系地址"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="家庭地址：">
                        <Input v-model="policyDetail.applicant.home_addr" placeholder="请输入家庭地址"></Input>
                    </Form-item>
                    </Col>
                    <!-- <Col span="8" offset="1">
                    <Form-item label="与被保人关系：" required>
                        <Input v-model="policyDetail.applicant.profession" placeholder="请输入与被保人关系"></Input>
                    </Form-item>
                    </Col> -->

                </Row>
            </Form>
        </div>
        <!-- 被保人 -->
        <div class="wrap">
            <h2>
                被保人信息
            </h2>
            <Form class="formdiv clearfix" v-for="(pitem,pindex) in policyDetail.insured" :key="pindex" :model="pitem" :ref="policyDetail.insured" :rules="ruleValidate" :label-width="140">
                <Row>
                    <Col span="23" offset="1">
                    <Tag type="dot" closable color="red" @on-close="insuredRemove(pindex)">删除被保人</Tag>
                    <Button type="text" style='color:blue' @click='insertInsured(pindex)'>导入被保人</Button>
                    <br/><br/>
                    </Col>
                </Row>
                <Row>                   
                    <Col span="8" offset="1">
                    <Form-item label="被保人姓名：" required>
                        <Input v-model="pitem.insuredName" placeholder="请输入姓名"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="身份证：" required>
                        <Input v-model="pitem.insuredNum" placeholder="请输入身份证号"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="手机号：" required>
                        <Input v-model="pitem.mobile" placeholder="请输入手机号"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="电子邮箱：">
                        <Input v-model="pitem.email" placeholder="请输入电子邮箱"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="联系地址：">
                        <Input v-model="pitem.addr" placeholder="请输入联系地址"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="家庭地址：">
                        <Input v-model="pitem.home_addr" placeholder="请输入家庭地址"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="与投保人关系：">
                        <Input v-model="pitem.relation" placeholder="请输入关系"></Input>
                    </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="23" offset="1">
                        <h2>
                           受益人信息
                        </h2>
                    </Col>
                </Row>
                <Row v-for="(item, index) in pitem.benefit" :key="index">    
                    <Row>
                        <Col span="4" offset="4">
                            <Button type="text" style='color:blue;padding-left:0' @click='insertBenefit(pindex,index)'>导入受益人</Button>
                        </Col>
                    </Row>              
                    <Col span="8" offset="1">
                    <FormItem label="受益人：">
                        <Input type="text" v-model="item.benefitName" placeholder="请输入受益人"></Input>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                    <Form-item label="身份证：">
                        <Input v-model="item.benefitNum" placeholder="请输入身份证号"></Input>
                    </Form-item>
                    </Col>
                    <Col span="8" offset="1">
                    <FormItem label="与被保人关系：">
                        <Input type="text" v-model="item.benefitRelation" placeholder="请选择被保人"></Input>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                    <FormItem label="受益比例：">
                        <Input type="text" v-model="item.benefitShare" placeholder="请输入受益比例"></Input>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                    <FormItem label="受益顺序：">
                        <Input type="text" v-model="item.benefitOrder" placeholder="请输入受益顺序"></Input>
                    </FormItem>
                    </Col>
                    <Col span="4" offset="1">
                    <Button type="error" @click="benefitRemove(pindex,index)">删除受益人</Button>
                    </Col>
                </Row>               
                <FormItem>
                    <Row>
                        <Col span="4">
                        <Button type="info" long @click="benefitAdd(pindex)" icon="plus-round">新增受益人</Button>
                        </Col>
                    </Row>
                </FormItem>
                <Row>
                    <Col span="17" offset="1">
                        <div class="line"></div>
                    </Col>
                </Row>
                <Row v-for="(item, index) in pitem.insurance" :key="index">
                    <Col span="8" offset="1">
                    <FormItem label="险种名称：" required>
                        <!-- <Input type="text" v-model="item.insuranceName" placeholder="请输入险种名称"></Input> -->
                        <!-- <Select v-model="item.insuranceId" placeholder="请选择险种" @on-change="changePro($event,pindex,index)">
                            <Option v-for="(value,key) in prolist" :key="key" :value="key">{{value.name}}</Option>
                            
                        </Select> -->
                        <Input type="text" v-model="item.insuranceName" placeholder="请导入险种" disabled></Input>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                        <Button type="primary" @click="openProModal(1,pindex,index)">从本公司导入</Button>
                        <Button type="primary" @click="openProModal(2,pindex,index)">从其他公司导入</Button>
                    </Col>
                    <div style="clear:both;height:0"></div>
                    <Col span="8" offset="1">
                    <FormItem label="保险期间：" required>
                        <!-- <Input type="text" v-model="item.insurancePeriod" placeholder="请输入保险期间"></Input> -->
                        <Select v-model="item.insurancePeriod" placeholder="请选择保险期间">
                            <Option v-for="item in item.proPeriod" :key="item" :value="item">{{item}}</Option>                           
                        </Select>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                    <FormItem label="交费方式：" required>
                        <!-- <Input type="text" v-model="item.payType" placeholder="请输入交费方式"></Input> -->
                        <Select v-model="item.payType" placeholder="请选择交费方式">
                            <Option v-for="item in item.propayType" :key="item" :value="item">{{item}}</Option>                           
                        </Select>
                    </FormItem>
                    </Col>                  
                    <Col span="8" offset="1">
                    <FormItem label="交费期间：" required>
                        <!-- <Input type="text" v-model="item.payPeriod" placeholder="请输入交费期间"></Input> -->
                        <Select v-model="item.payPeriod" placeholder="请选择交费期间">
                            <Option v-for="item in item.propayPeriod" :key="item" :value="item">{{item}}</Option>                           
                        </Select>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                    <FormItem label="保险金额(元)/档次/日额(元/天)：" :label-width='240' required>
                        <Input type="text" v-model="item.amount" placeholder="请输入保额"></Input>
                    </FormItem>
                    </Col>
                     <Col span="8" offset="1">
                    <FormItem label="保费：" required>
                        <Input type="text" v-model="item.rate" placeholder="请输入保费"></Input>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                    <FormItem label="生效日期：" required>
                        <DatePicker type="date" style="width: 240px" v-model="item.inception_date_format" format="yyyy-MM-dd" @on-change="(datetime) =>{ insureInceptionDateChange(datetime, pindex,index)}" placeholder="选择日期"></DatePicker>
                    </FormItem>
                    </Col>
                    <Col span="8" offset="1">
                    <FormItem label="状态：" required>
                        <!-- <Input type="text" v-model="item.status" placeholder="请输入状态"></Input> -->
                        <Select v-model="item.status" placeholder="请选择状态" >
                            <Option  value="正常">正常</Option>
                            <Option  value="终止">终止</Option>
                        </Select>
                    </FormItem>
                    </Col>
                   <Col span="17" offset="1">
                        <div class="line"></div>
                    </Col>
                    <div v-for="(sitem, sindex) in item.interest" :key="sindex">
                        <Col span="8" offset="1">
                        <FormItem label="保障责任：" required>
                            <Input type="text" v-model="sitem.interestName" placeholder="请输入保障责任"></Input>
                        </FormItem>
                        </Col>
                        <Col span="8" offset="1">
                        <FormItem label="保险金额：" required>
                            <Input type="text" v-model="sitem.interestMoney" placeholder="请输入保险金额"></Input>
                        </FormItem>
                        </Col>
                        <Col span="4" offset="1">
                        <Button type="error" @click="interestRemove(pindex,index,sindex)">删除保险责任</Button>
                        </Col>
                    </div>
                    <Col span="22"  offset="2">
                    <Button type="info" @click="interestAdd(pindex,index)">新增保险责任</Button>
                    </Col>
                    <Col span="15" offset="9">
                    <Button type="error" @click="proRemove(pindex,index)">删除险种</Button>
                    </Col>
                    <Col span="17" offset="1">
                        <div class="line"></div>
                    </Col>
                </Row>
                <FormItem>
                    <Row>
                        <Col span="4">
                        <Button type="info" long @click="proAdd(pindex)" icon="plus-round">新增险种</Button>
                        </Col>
                    </Row>
                </FormItem>
            </Form>
            
            <div>
                <Row>
                    <Col span="6" offset="7">
                    <Button type="primary" long @click="insuredAdd()" icon="plus-round">新增被保人</Button>
                    </Col>
                </Row>
            </div>
        </div>
       
        <Row class="mt20 mb20">
            <Col span="6" offset="8">
            <Button type="primary" @click="handleSubmit()">提交</Button>
            </Col>
        </Row>
        <Modal v-model="insertModal" width="600" title="添加客户信息">
            <Form>
                <Row>
                    <Col span="8">
                    <Form-item>
                        <Input type="text" style="width:200px" v-model="modalSearch.name" placeholder="请输入客户姓名"></Input>
                    </Form-item>
                    </Col>
                    <Col span="2" offset="1">
                    <Form-item>
                        <Button type="primary" @click="searchCustomer">搜索</Button>
                    </Form-item>
                    </Col>
                </Row>
            </Form>
            <Table width="552" height="400" border :columns="columns_customer" :data="data_customer"></Table>
            
            <div slot="footer">
            </div>
        </Modal>
        <Modal v-model="insertModal1" width="600" title="添加险种信息">
            <Form>
                <Row>
                    <Col span="8">
                    <Form-item>
                        <Input type="text" style="width:200px" v-model="modalSearch1.name" placeholder="请输入险种名称"></Input>
                    </Form-item>
                    </Col>
                    <Col span="2" offset="1">
                    <Form-item>
                        <Button type="primary" @click="insertpro">搜索</Button>
                    </Form-item>
                    </Col>
                </Row>
            </Form>
            <Table width="552" height="400" border :columns="columns_pro" :data="data_pro"></Table>
            
            <div slot="footer">
            </div>
        </Modal>
    </div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { getPolicyDetail, savePolicy , updatePolicy , policyOcr , insertPro} from '../../api/policy'
import { getComlistall } from '../../api/parter';
import { getCustomerListAll } from '../../api/customer';
import { getProListAll } from '../../api/product_v2';

export default {
    data() {
        return {
            file: null,
            basefile:'',
            loadingStatus: false,
            cou:-1,
            inceptionDate:'',
            modalSearch:{},
            modalSearch1:{},
            policyDetail: {
                inception_date_format:'',
                applicant: {

                },
                insured: [
                    {
                        benefit: [
                            {
                                benefitName: '',
                                benefitShare: '',
                                benefitOrder: ''
                            }
                        ],
                        insurance: [
                            {
                                insuranceName: '',
                                insuranceId: '',
                                insurancePeriod: '',
                                amount: '',
                                payPeriod: '',
                                rate: '',
                                interest:[
                                    {
                                        interestName:'',
                                        interestMoney:''
                                    }
                                ]

                            }
                        ]
                    }
                ]
            },
            insertModal:false,
            insertModal1:false,
            protype:'',
            insertType:1,    //1:投保人，2：被保人 3：受益人
            insertinsuredIndex:0,  //导入数据的被保人在数组中的下标
            insertBenefitIndex:0,  //导入数据的受益人在数组中的下标
            ruleValidate: {
                num: [
                    { required: true, message: '保单编号不能为空', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '险种名称不能为空', trigger: 'blur' },
                ],
                cname: [
                    { required: true, message: '公司名称不能为空', trigger: 'blur' }
                ],
                agent_code: [
                    { required: true, message: '投保人编号不能为空', trigger: 'blur' }
                ],
                // pay_period: [
                //     { required: true, message: '缴费期间不能为空', trigger: 'change' }
                // ],
                start_date_format: [
                    { required: true, type: 'date', message: '生效日期不能为空', trigger: 'blur' }
                ],
                // rate: [
                //     { required: true, message: '保费不能为空', trigger: 'change' }
                // ],
                // commission_rate: [
                //     { required: true, message: '佣金系数不能为空', trigger: 'change' }
                // ],
            },
            columns_customer:[
                {
                    title: '姓名',
                    key: 'true_name',
                    width: 110,
                    align: 'center'
                },
                {
                    title: '手机号',
                    key: 'mobile',
                    width: 120,
                    align: 'center'
                },
                {
                    title: '身份证号',
                    key: 'id_card',
                    width: 210,
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    align: 'center',
                    width: 110,
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                on:{
                                    click:() => {
                                        this.insertData(params.row.true_name,params.row.mobile,params.row.id_card);
                                    }
                                }
                            }, '添加'),
                        ]);
                    }
                }
            ],
            data_customer:[
                
            ],
            columns_pro:[
                {
                    title: '名称',
                    key: 'name_format',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    align: 'center',
                    width: 110,
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                on:{
                                    click:() => {
                                        this.insertProData(params.row.name,params.row.id,params.row.pay_period,params.row.pay_type,params.row.period);
                                    }
                                }
                            }, '添加'),
                        ]);
                    }
                }
            ],
            data_pro:[
                
            ],
            prolist:{},
            proPeriod:[],
            propayType:[],
            propayPeriod:[],
            comlist:[],
            pindex:'',
            index:''
        }
    },
    components: {
        productTitle
    },
    created() {
        this.getPro()       
        this.getCustomer()       
        this.getComlist()
    },
    methods: {
        //打开搜索险种modal
        openProModal(type,pindex,index){
            if(type != this.protype){
                this.data_pro = []
            }
            this.insertModal1 = true
            this.protype = type
            this.pindex = pindex
            this.index = index
        },
        //导入保单
        insertpro(){
            let params = {
                type:this.protype,
                name:this.modalSearch1.name                
            }
            new Promise((resolve, reject) => {
                insertPro(params).then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {                        
                        this.data_pro = response.data.data
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //保单识别
        handleUpload (file) {
            this.file = file;
            var reader = new FileReader()
            reader.onload = (e) => {
                this.basefile = e.target.result
            }
            reader.readAsDataURL(file)
            return false;
        },
        upload () {
            this.loadingStatus = true;
            let _params = {
                img_base64:this.basefile
            }
            new Promise((resolve, reject) => {
                policyOcr(_params).then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {                        
                        this.file = null;
                        this.basefile = '';
                        this.loadingStatus = false;
                        this.$Message.success('上传成功')
                        this.policyDetail = Object.assign({},response.data.data)
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
            
        },
        //获取合作商列表
        getComlist(){
            new Promise((resolve, reject) => {
                getComlistall().then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.comlist = response.data.data
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        getPro() {           
            new Promise((resolve, reject) => {
                getProListAll().then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.prolist = response.data.data
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve(this.getDetail());
                }).catch(error => {
                    reject(error);
                });
            });
        },
        changePro(value,pindex,index) {
            var _Period = this.prolist[value].period
            var _payType = this.prolist[value].pay_type
            var _payPeriod = this.prolist[value].pay_period
            let _name = this.prolist[value].name
            // this.proPeriod[index] = _Period
            // this.propayType[index] = _payType
            // this.propayPeriod[index] = _payPeriod
            this.policyDetail.insured[pindex].insurance[index].proPeriod = _Period
            this.policyDetail.insured[pindex].insurance[index].propayType = _payType
            this.policyDetail.insured[pindex].insurance[index].propayPeriod = _payPeriod
            this.policyDetail.insured[pindex].insurance[index].insuranceName = _name
        },
        getDetail() {
            if(this.$route.params.id && this.$route.params.id != 0){
                let _params = {
                    id:this.$route.params.id
                }
                this.cou = 1
                new Promise((resolve, reject) => {
                    getPolicyDetail(_params).then(response => {
                        console.log(response.data)
                        if (response.data.err == 0) {
                            this.policyDetail = response.data.data;
                            if(this.policyDetail.inception_date){
                                this.policyDetail.inception_date_format = this.policyDetail.inception_date;
                                this.inceptionDate = new Date(this.policyDetail.inception_date)
                            }
                            // this.policyDetail.insured.forEach(function(value,index){
                            //     value.insurance.forEach(function(v,k){
                            //          v.inception_date_format = v.inception_date;
                            //          v.inception_date = new Date(v.inception_date)
                            //     })
                            // })
                        } else {
                            this.$Message.error(response.data.info);
                        }
                        resolve();
                    }).catch(error => {
                        reject(error);
                    });
                });
            }else{
                this.cou = -1
            }
           
        },

        //添加被保人
        insuredAdd() {
            this.policyDetail.insured.push(
                {
                    benefit: [
                        {
                            benefitName: '',
                            benefitShare: '',
                            benefitOrder: ''
                        }
                    ],
                    insurance: [
                        {
                            insuranceName: '',
                            insuranceId: '',
                            insurancePeriod: '',
                            amount: '',
                            payPeriod: '',
                            rate: '',
                            commission_rate: '',
                            interest:[
                                {
                                    interestName:'',
                                    interestMoney:''
                                }
                            ]
                        }
                    ]
                }
            );
        },
        // 删除被保人
        insuredRemove(pindex) {
            this.policyDetail.insured.splice(pindex, 1);
        },
        //添加受益人
        benefitAdd(pindex) {
            this.policyDetail.insured[pindex].benefit.push({
                value: ''
            });
        },
        //删除受益人
        benefitRemove(pindex, index) {
            this.policyDetail.insured[pindex].benefit.splice(index, 1);
        },
        //添加险种
        proAdd(pindex) {
            this.policyDetail.insured[pindex].insurance.push({
                interest:[
                    {
                        interestName:'',
                        interestMoney:''
                    }
                ]
            });
        },
        // 删除险种
        proRemove(pindex, index) {
            this.policyDetail.insured[pindex].insurance.splice(index, 1);
        },
        //添加保险责任
        interestAdd(pindex,index) {
            this.policyDetail.insured[pindex].insurance[index].interest.push({
                interestName:'',
                interestMoney:''
            });
        },
        //删除保险责任
        interestRemove(pindex,index,sindex) {
            this.policyDetail.insured[pindex].insurance[index].interest.splice(sindex, 1);
        },
        //提交保存
        handleSubmit() {
            //  this.$refs[this.employeeDetail].validate((valid) =>{
            //     if (valid) {
                if(this.cou == -1){
                    new Promise((resolve, reject) => {
                        savePolicy(this.policyDetail).then(response => {
                            if (response.data.err == 0) {
                                // this.$router.push({name:'policy_list'})
                                this.$Message.success(response.data.info);
                                this.$router.go(-1);
                                
                            } else {
                                this.$Message.error(response.data.info);
                            }
                            resolve();
                        }).catch(error => {
                            reject(error);
                        });
                    });
                }else{
                    new Promise((resolve, reject) => {
                        updatePolicy(this.policyDetail).then(response => {
                            if (response.data.err == 0) {
                                // this.$router.push({name:'policy_list'})
                                this.$Message.success(response.data.info);
                                this.$router.go(-1);
                                
                            } else {
                                this.$Message.error(response.data.info);
                            }
                            resolve();
                        }).catch(error => {
                            reject(error);
                        });
                    });
                }
                    
            //     } 
            // });
        },
        // 监测datepicker的on-change时间，date为返回值，是格式化后的日期
        inceptionDateChange(date) {
            this.policyDetail.inception_date = date;
        },
        insureInceptionDateChange(date, pindex,index) {
            this.policyDetail.insured[pindex].insurance[index].inception_date = date;
        },
        //搜索客户
        searchCustomer(){
            this.getCustomer();
        },
        //导入投保人
        insertApp(){
            this.insertType = 1;
            this.insertModal = true;
        },
        //导入被保人
        insertInsured(insuredIndex){
            this.insertType = 2;
            this.insertinsuredIndex = insuredIndex;
            this.insertModal = true;
        },
        //导入受益人
        insertBenefit(insuredIndex,benefitIndex){
            this.insertType = 3;
            this.insertinsuredIndex = insuredIndex;
            this.insertBenefitIndex = benefitIndex;
            this.insertModal = true;
        },
        //导入全部客户
        getCustomer(){
            let _params={
                true_name:this.modalSearch.name
            };
            new Promise((resolve, reject) => {
                    getCustomerListAll(_params).then(response => {
                        console.log(response.data)
                        if (response.data.err == 0) {
                            this.data_customer = response.data.data
                        } else {
                            this.$Message.error(response.data.info);
                        }
                        resolve();
                    }).catch(error => {
                        reject(error);
                    });
                });
        },
        //插入客户数据
        insertData(true_name,mobile,id_card){
            if(this.insertType == 1) {
                this.policyDetail.applicant.appName = true_name;
                this.policyDetail.applicant.appMobile = mobile;
                this.policyDetail.applicant.appNum = id_card;
                this.insertModal = false;
            } else if(this.insertType == 2) {
                this.policyDetail.insured[this.insertinsuredIndex].insuredName = true_name;
                this.policyDetail.insured[this.insertinsuredIndex].mobile = mobile;
                this.policyDetail.insured[this.insertinsuredIndex].insuredNum = id_card;
                this.insertModal = false;
            } else if(this.insertType == 3) {
                this.policyDetail.insured[this.insertinsuredIndex].benefit[this.insertBenefitIndex].benefitName = true_name;
                this.policyDetail.insured[this.insertinsuredIndex].benefit[this.insertBenefitIndex].benefitNum = id_card;
                this.insertModal = false;
            }
            
        },
        //插入险种信息
        insertProData(name,id,pay_period,pay_type,period){
            this.insertModal1 = false
            this.policyDetail.insured[this.pindex].insurance[this.index].proPeriod = period
            if(pay_type){
                this.policyDetail.insured[this.pindex].insurance[this.index].propayType = pay_type
            }else{
                this.policyDetail.insured[this.pindex].insurance[this.index].propayType =[
                    '趸交','期交'
                ]
            }            
            this.policyDetail.insured[this.pindex].insurance[this.index].propayPeriod = pay_period
            this.policyDetail.insured[this.pindex].insurance[this.index].insuranceName = name
            this.policyDetail.insured[this.pindex].insurance[this.index].insuranceId = id
        }
    }
}
</script>
